package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.blog.common.pojo.dataobject.BlogInfo;
import com.example.blog.common.pojo.request.AddBlogParam;
import com.example.blog.common.pojo.request.UpBlogParam;
import com.example.blog.common.pojo.response.BlogInfoResponse;
import com.example.blog.common.utils.BeanConverse;
import com.example.blog.mapper.BlogInfoMapper;
import com.example.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/25
 * Time: 19:14
 */
@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogInfoMapper blogMapper;

    /**
     * 获取博客列表
     * @return
     */
    @Override
    public List<BlogInfoResponse> getList() {
        // 查询数据库
        List<BlogInfo> blogInfos = blogMapper.selectList(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getDeleteFlag, 0).orderByDesc(BlogInfo::getId));
        // 转换
        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream().map(blogInfo -> {
            return BeanConverse.trans(blogInfo);
        }).collect(Collectors.toList());
        return blogInfoResponses;

    }

    /**
     * 根据 id 获取博客详情
     * @param blogId
     * @return
     */
    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = blogMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId).eq(BlogInfo::getDeleteFlag, 0));
        return BeanConverse.trans(blogInfo);
    }

    @Override
    public Boolean addBlog(AddBlogParam param) {
        // 对象转换 AddBlogParam -> BlogInfo
        BlogInfo blogInfo = BeanConverse.trans(param);
        // 插入博客
        try {
            int result = blogMapper.insert(blogInfo);
            if (result == 1) {
                return true;
            }
        } catch (Exception e) {
            log.error("插入博客失败, e: ", e);
        }
        return false;
    }

    @Override
    public Boolean updateBlog(UpBlogParam param) {
        // 对象转换 UpBlogParam -> BlogInfo
        BlogInfo blogInfo = BeanConverse.trans(param);
        // 更新博客
        return update(blogInfo);
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        return update(blogInfo);
    }

    /**
     * 更新博客数据
     * @param blogInfo
     * @return
     */
    public Boolean update(BlogInfo blogInfo) {
        try {
            int result = blogMapper.updateById(blogInfo);
            if (result == 1) {
                return true;
            }
        } catch(Exception e) {
            log.error("更新博客失败, e: ", e);
        }
        return false;
    }


}
